/**
 * @file        caidan.c
 * @author      JinChengLong (https://gitee.com/Datejin/)
 * @brief       小车菜单界面
 * @version     0.1
 * @date        2023
 * 
 * @copyright   Copyright © 2025 by JinChengLong 
 * 
 * All rights reserved.
 * Unauthorized copying, reproduction, or redistribution of this file via any medium is strictly prohibited.  
 * Proprietary and confidential.
 *   
 */


 

#include "caidan.h"
#include "OLED.h"
#include "usart.h"
#include "DHT.h"
#include "MPU6050.h"
#include "hcr.h"
#include "motor.h"
#include "servo.h"
#include "motorf.h"
#include <stdio.h>
#include <stdlib.h>



uint8_t tagup=0, tagdown=0, tagleft =0, tagright=0, c1=1, ji1=1, m1=3, ki1=3, yema=1, jiao=0, dong=0;
uint8_t zuo, you, qian, hou, qian1;
uint8_t a=63, b=64, c=63, d=64;
int16_t v=0, op=0;
int16_t ax0, ay0, az0, gx0, gy0, gz0;
extern uint16_t n;

/*，版权归金程龙所有，
于2014.1.16  17：18：56完成*/
/**
 * @brief 光标下移
 * 
 */
void  pointdown(void)
{
	
	
	tagdown =tagdown +1;
	tagup =4-tagdown ;
	
	
	if(tagdown==4)
	{
		tagdown =0;
		ji1 =1;
		c1=1+14*3;
	}
	else
	{
		c1=1+14*(tagdown -1);
		ji1 =1+14*tagdown;
		
		
	}
	
	OLED_ReverseArea(ki1,c1,13,13);
	OLED_ReverseArea(ki1,ji1,13,13);//最新
}

/**
 * @brief 光标上移
 * 
 */
void  pointup(void)
{
	
	
	tagup =tagup +1;
	tagdown =4-tagup ;
	
	
	if(tagup==1)
	{
		
		c1 =1;
		ji1=1+14*3;
	}
	else if(tagup==5)
	{
		tagup =1;
		tagdown=4;
		c1 =1;
		ji1=1+14*3;
	}
	else
	{
		c1 =1+14*(5-tagup);
		ji1=1+14*(4-tagup );
	}
	

	OLED_ReverseArea(ki1,c1,13,13);
	OLED_ReverseArea(ki1,ji1,13,13);//最新
	
}





/**
 * @brief 光标左移
 * 
 */
void pointleft(void)
{
	tagleft =tagleft +1;
	tagright =9-tagleft ;
	
	if(tagleft==9)
	{
		tagleft =0;
		ki1 =3;
		m1=3+14*8;
	}
	else 
	{
		m1=3+14*(tagleft -1);
		ki1 =3+14*tagleft;
		
		
	}
	OLED_ReverseArea(m1,ji1,13,13);
	OLED_ReverseArea(ki1,ji1,13,13);
}


/**
 * @brief 光标右移
 * 
 */
void pointright(void)
{
	
	tagright =tagright +1;
	tagleft =9-tagright;

	if(tagright==1)
	{
		
		m1 =3;
		ki1=3+14*8;
	}
	else  if(tagright==10)
	{
		tagright=1;
		tagleft=9;
		m1=3;
		ki1=3+14*8;
	} 
	else
	{
		m1=3+14*(10-tagright );
		ki1=3+14*(9-tagright);
		
		
	}
	
	OLED_ReverseArea(m1,ji1,13,13);
	OLED_ReverseArea(ki1,ji1,13,13);
}
	
	
		
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
/*图标绘制*/	
	
/*1*/	
/**
 * @brief 温湿度图标
 * 
 */
void TandD(void)
{
	//OLED_DrawPoint(6,24);
	OLED_Clear();
	
	while(1)
	{
		//OLED_DrawPoint(6,24);
		OLED_ShowChinese(2,2,"温度");
		OLED_ShowString(75,30,".",8);
		OLED_ShowString(75,2,".",8);
		OLED_ShowChinese(2,30,"湿度");
		OLED_Update();
		if(Read_DHT11()==1);
		{
			OLED_ShowNum(60,2,T,2,8);OLED_ShowNum(85,2,t,1,8);
			OLED_ShowNum(60,30,H,2,8);OLED_ShowNum(85,30,h,1,8);
			
			
	        usart_sendstring("@");
			usart_sendbit(H);
			usart_sendbit(h);
			usart_sendbit(T);
			usart_sendbit(t);
			usart_sendstring(" .");
			if(strcmp(Serial_RxPacket, "STOP") == 0)
			{
				Delay_ms (200);
				while(1)
				{
					OLED_ShowNum(60,2,T,2,8);OLED_ShowNum(85,2,t,1,8);
					OLED_ShowNum(60,30,H,2,8);OLED_ShowNum(85,30,h,1,8);
			
					if(strcmp(Serial_RxPacket, "TandD")==0)
						break;
				}
			}
			yema=2;
			if(strcmp(Serial_RxPacket, "R") == 0)
			{
				fanhuizhucaidan();
				break;
			}
		}
		OLED_Update();
	}
}


/*2*/	
/**
 * @brief 遥控小车图标
 * 
 */
void xiaoche(void)
{
	uint8_t h0=0,z0=0,y0=0,q0=0,circle=0;
	OLED_Clear();
	OLED_Update();
	while(1)
	{
//		if(usart_getRxflag()==1)
//		{
			if(strcmp(Serial_RxPacket, "da") == 0)
			{
				
				q0=100;
//				if(q0>=100){q0=50;}
//				if(q0<=50){q0=50;}

				motor_speed(q0,q0);
				motorf_speed(q0,q0);
				Delay_ms (300);
		
			}
			else if(strcmp(Serial_RxPacket, "z") == 0)
			{
				
				h0=0;y0=0;q0=0;
				
				z0=100;
				motor_speed(0,z0);
				motorf_speed(0,z0);
				Delay_ms (300);
			}
			else if(strcmp(Serial_RxPacket, "y") == 0)
			{
				
				h0=0,z0=0,q0=0;
			
				y0=100;
				motor_speed(y0,0);
				motorf_speed(y0,0);
				Delay_ms (300);
			}
			else if(strcmp(Serial_RxPacket, "i") == 0)
			{
				motor_speed(0,0);
				motorf_speed(0,0);
		
				h0=0,z0=0,y0=0,q0=0;	
				Delay_ms (300);				
			}
			else if(strcmp(Serial_RxPacket, "H") == 0)
			{
				
				z0=0,q0=0,y0=0;
			
				h0=100;
				motor_speed(-h0,-h0);
				motorf_speed(-h0,-h0);
				Delay_ms (300);
			}
			else if(strcmp(Serial_RxPacket, "C") == 0)
			{
				
				z0=0,q0=0,y0=0;
				
				circle=100;
				motor_speed(-circle,circle);
				motorf_speed(-circle,circle);
				Delay_ms (300);
			}
			else if(strcmp(Serial_RxPacket, "Cr") == 0)
			{
				
				z0=0,q0=0,y0=0;
			
				circle=100;
				motor_speed(circle,-circle);
				motorf_speed(circle,-circle);
				Delay_ms (300);
			}
			else 
			{
				
				motor_speed(0,0);
				motorf_speed(0,0);
				
				
				
				
				
				
			}
			OLED_ShowChinese(1,1,"小车调控");
			OLED_ShowChinese(0,20,"前进");
			OLED_ShowChinese(60,20,"后退");
			OLED_ShowChinese(0,40,"右转");
			OLED_ShowChinese(60,40,"左转");
			OLED_ShowNum(30,20,q0,3,8);	
			OLED_ShowNum(90,20,h0,3,8);
			OLED_ShowNum(30,40,y0,3,8);
			OLED_ShowNum(90,40,z0,3,8);
			
			OLED_Update();
			if(strcmp(Serial_RxPacket, "R") == 0)
			{
				fanhuizhucaidan();
				break;
			}
//		}
	}
}



/*3*/
/**
 * @brief 陀螺MPU6050图标
 * 
 * @return * void 
 */
void zhuti(void)
{
	OLED_Clear();
	while(1)
	{
		MPU6050_getdata(&ax0,&ay0,&az0,&gx0,&gy0,&gz0);
	
		
		OLED_ShowChinese(1,1,"主体");
		OLED_ShowFloatNum(1,1,ax0*9.8/2048,2,2,8);
		OLED_ShowFloatNum(1,20,ay0*9.8/2048,2,2,8);
		OLED_ShowFloatNum(1,40,az0*9.8/2048,2,2,8);
		OLED_ShowSignedNum(60,1,gx0/16.4,4,8);
		OLED_ShowSignedNum(60,20,gy0/16.4,4,8);
		OLED_ShowSignedNum(60,40,gz0/16.4,4,8);
		if(strcmp(Serial_RxPacket, "R") == 0)
		{
			fanhuizhucaidan();
			break;
		}
		OLED_Update();
	}
				
}

/*4*/	
/**
 * @brief 超声波位置图标
 * 
 */
void chaoshengbo(void )
{
	HCR04_init();
	OLED_Clear ();
	while(1)
	{
		OLED_ShowFloatNum(1,1,HCR04_m(1),3,3,8);
		OLED_Update();
		
		if(strcmp(Serial_RxPacket, "R") == 0)
		{
			fanhuizhucaidan();
			break;
		}
	}

}

/*5简易路线图标*/	
//void luxian(void)
//{
//	uint8_t m;
//	OLED_Clear ();
//	//OLED_ShowNum(40,1,qian ,4,8);
//	//OLED_ShowNum(40,15,zuo  ,4,8);
//	//OLED_ShowNum(40,45,you ,4,8);
//	//OLED_ShowNum(40,30,hou  ,4,8);
//	
//	HCR04_m (1);
//	if(qian1!=0)
//	{
//		d=d-qian1;
//	
//		OLED_DrawLine(a,b,c,d);
//		OLED_Update();
//	}
//	else if(qian1==0)
//	{
//		if(zuo!=0)
//		{
//			b=64-qian1 ;c=c-1;
//			OLED_DrawLine(a,b,c,d);
//			OLED_Update();
//
//		}
//		else if(you !=0)
//		{
//			b=b-qian1 ;c=c+1;
//			OLED_DrawLine(a,b,c,d);
//			OLED_Update();
//
//			
//		}
//			m=c;c=a;a=m;
//			m=d;d=b;m=b;
//		
//		
//	}
//	//OLED_Update ();
//	
//	
//}

/**/
/**
 * @brief 喇叭图标
 * 
 */
void laba(void )
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);			//初始化PA0引脚
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;			//复用上拉输入用定时器控制引脚
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	GPIO_SetBits(GPIOA,GPIO_Pin_7 );
	jiao=jiao+1;
	if(jiao==1)
	{
		GPIO_ResetBits (GPIOA,GPIO_Pin_7);
		
	}
	else if (jiao!=1)
	{
		jiao=0;
		GPIO_SetBits(GPIOA,GPIO_Pin_7 );
	}
	else if(strcmp(Serial_RxPacket, "R") == 0)
	{
		fanhuizhucaidan3();
	}
	
	
}
	
/**/
/**
 * @brief 小车自动避障图标
 * 
 */
void xiaoche_xunji(void)
{
	int16_t speedz1,speedy1 ;
	dong=dong+1;
	if(dong==1)
	{
		speedz1=50;
		speedy1=50;
		HCR04_init();
		servo_angle(90);
		motor_speed(speedz1,speedy1);
		motorf_speed(speedz1,speedy1);
		
		while(1)
		{
			Delay_ms (20);
			if(HCR04_m(1)/1<60)
			{
				Delay_ms (500);
				servo_angle(0);
				Delay_ms (500);
				if(HCR04_m(1)/1<60)
				{
					Delay_ms (500);
					servo_angle(180);
					Delay_ms (500);
	
					if(HCR04_m(1)/1<60)
					{	
						Delay_ms (500);
						servo_angle(90);
						Delay_ms (500);
						
						speedz1=-70;speedy1=-70;
						motor_speed(speedz1,speedy1);
						motorf_speed(speedz1,speedy1);
						Delay_ms (1000);
						
						speedz1=0;speedy1=100;
						motor_speed(speedz1,speedy1);
						motorf_speed(speedz1,speedy1);
						Delay_ms (600);
					}
					else 
					{
						Delay_ms (500);
						servo_angle(90);
						Delay_ms (500);
						speedz1=0;speedy1=100;
						motor_speed(speedz1,speedy1);
						motorf_speed(speedz1,speedy1);
						Delay_ms (1000);
					}
				}
				else 
				{
					Delay_ms (500);
					servo_angle(90);
					Delay_ms (500);
					motor_speed(100,0);
					motorf_speed(100,0);
					n=0;
					you=you+1;
					Delay_ms (1000);
				}
			}
			else 
			{
				speedz1=50;speedy1=50;
				motor_speed(speedz1,speedy1);
				motorf_speed(speedz1,speedy1);
			}
			
			

			
			
			if(strcmp(Serial_RxPacket, "I") == 0)
			{
				dong=dong +1;
				motor_speed (0,0);
				motorf_speed (0,0);

				break;
			}
			
		}	
	}
	else if (dong!=1)
	{
		dong=0;
		motor_speed (0,0);
	}
}













/**/	

/**
 * @brief 主菜单复位
 * 
 */
void fanhuizhucaidan(void)
{
	OLED_Clear();		
	OLED_ShowImage(3,1,13,13,shui);
	OLED_ShowImage(3,15,13,13,dian);
	OLED_ShowImage(3,29,13,13,wen);
	OLED_ShowImage (3,43,13,13,chaosheng);
	OLED_ShowImage (17,1,13,13,music);
	OLED_ShowImage (17,15,13,13,zidongtu);
	OLED_ShowImage (17,29,13,13,luxiantu);
	tagup=0;tagdown =0;tagleft=0;tagright=0;
	OLED_ReverseArea(3,1,13,13);
	OLED_Update();
}

/**
 * @brief 菜单1
 * 
 */
void fanhuizhucaidan2(void)
{
	OLED_Clear();		
	OLED_ShowImage(3,1,13,13,shui);
	OLED_ShowImage(3,15,13,13,dian);
	OLED_ShowImage(3,29,13,13,wen);
	OLED_ShowImage (3,43,13,13,chaosheng);
	OLED_ShowImage (17,1,13,13,music);
	OLED_ShowImage (17,15,13,13,zidongtu);
	OLED_ShowImage (17,29,13,13,luxiantu);
	OLED_ReverseArea(17,15,13,13);
	OLED_Update();
}


/**
 * @brief 菜单2
 * 
 */
void fanhuizhucaidan3(void)
{
	OLED_Clear();		
	OLED_ShowImage(3,1,13,13,shui);
	OLED_ShowImage(3,15,13,13,dian);
	OLED_ShowImage(3,29,13,13,wen);
	OLED_ShowImage (3,43,13,13,chaosheng);
	OLED_ShowImage (17,1,13,13,music);
	OLED_ShowImage (17,15,13,13,zidongtu);
	OLED_ShowImage (17,29,13,13,luxiantu);
	OLED_ReverseArea(17,1,13,13);
	OLED_Update();
}


